package com.ybkj.daijia.api.weixin.handler.impl;

import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.WxSetting;
import com.ybkj.daijia.api.weixin.WxMsgType;
import com.ybkj.daijia.api.weixin.entity.WxBaseMsgEntity;
import com.ybkj.daijia.api.weixin.entity.WxBaseRespEntity;
import com.ybkj.daijia.api.weixin.entity.WxItemPicDescEntity;
import com.ybkj.daijia.api.weixin.entity.WxMsgEventEntity;
import com.ybkj.daijia.api.weixin.entity.WxRespPicDescEntity;
import com.ybkj.daijia.server.service.MemberAccountService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.WxBindService;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WxAccountEventHandler extends AbstractWxEventHandler {

    @Autowired
    private WxBindService wxBindService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private MemberService memberService;

    @Autowired
    private MemberAccountService memberAccountService;


    @Override
    public boolean canHandle(WxBaseMsgEntity msg) {
        if (super.canHandle(msg)) {
            if ("myaccount".equals(((WxMsgEventEntity) msg).getEventKey())) {
                return true;
            }
        }

        return false;
    }

    @Override
    public WxBaseRespEntity handleEvent(WxMsgEventEntity msg,
        Map<String, Object> context, WxBaseRespEntity resp) {

        //图文消息
        WxRespPicDescEntity news = new WxRespPicDescEntity();

        List<WxItemPicDescEntity> articles = new LinkedList<WxItemPicDescEntity>();

        WxItemPicDescEntity item = new WxItemPicDescEntity();

        WxSetting setting = settingUtils.getWx();

        String pic = String.format("%sassets/img/my_account.png", setting.getWxServer());
        String url = String
            .format("%swx/v1/myAccount?wxuser=" + msg.getFromUserName(), setting.getWxServer());

        item.setTitle("我的账户");
        item.setPicUrl(pic);
        item.setUrl(url);
        item.setDescription("我的账户");
        articles.add(item);

        news.setArticles(articles);
        news.setCreatedDate(new Date());
        news.setCreateTime(news.getCreatedDate().getTime());
        news.setFromUserName(msg.getToUserName());
        news.setToUserName(msg.getFromUserName());
        news.setFuncFlag(0);
        news.setMsgType(WxMsgType.news.name());

        return news;

//			WxBind bind = wxBindService.findByWxuser(msg.getFromUserName());
//			
//			if(null == bind || bind.getStatus() == WxBind.WXBIND_STATUS_WAIT){
//				
//				WxSetting setting = settingUtils.getWx();
//				
//				WxRespTextEntity text = new WxRespTextEntity();
//				text.setContent(setting.getWxNoBindAccount());//没有绑定账号提醒
//				text.setCreatedDate(new Date());
//				text.setCreateTime(text.getCreatedDate().getTime());
//				text.setFromUserName(msg.getToUserName());
//				text.setToUserName(msg.getFromUserName());
//				text.setFuncFlag(0);
//				text.setMsgType(WxMsgType.text.name());
//				
//				return text;
//			}
//			else{
//				
//				MemberAccount account = memberAccountService.findByPhone(bind.getPhone());
//				
//				Member member = memberService.findOne(account.getMemberId());
//				
//				String memberType = member.getType();
//				
//				String message = StringUtils.EMPTY;
//				
//				if(memberType.equals(Member.MEMBER_TYPE_COMPANY)){
//					message = "您的账户类型为：企业用户。\n账户余额为："+member.getVirtual().doubleValue()+"。\n";
//				}
//				else if(memberType.equals(Member.MEMBER_TYPE_VIP)){
//					message = "您的账户类型为：VIP用户。\n账户余额为："+member.getVirtual().doubleValue()+"。\n";
//				}
//				else{
//					message = "您的账户类型为：个人用户。\n账户余额为："+member.getVirtual().doubleValue()+"。\n";
//				}
//				
//				WxRespTextEntity text = new WxRespTextEntity();
//				text.setContent(message);//没有绑定账号提醒
//				text.setCreatedDate(new Date());
//				text.setCreateTime(text.getCreatedDate().getTime());
//				text.setFromUserName(msg.getToUserName());
//				text.setToUserName(msg.getFromUserName());
//				text.setFuncFlag(0);
//				text.setMsgType(WxMsgType.text.name());
//				
//				return text;
//			}
    }


}
